package com.whfc.fse.pushPlatform;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.HttpUtil;
import com.whfc.fse.entity.FseSync;
import com.whfc.fse.pushPlatform.model.ZHGJFseCraneDTO;
import com.whfc.fse.pushPlatform.model.ZHGJFseCraneParamDTO;
import com.whfc.fse.pushPlatform.model.ZHGJFseLiftDTO;
import com.whfc.fse.pushPlatform.model.ZHGJFseLiftParamDTO;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
/**
 * 智慧工匠特种设备推送api
 */
public class ZHGJEnvApi {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());



    /**
     * 获取token
     * @return
     */
    public String getToken(String url,String deviceSn) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceSn", deviceSn);
        jsonObject.put("deviceType", 1);
        String getToken = "/api/device/token";
        String result = HttpUtil.doPost(url + getToken, JSONObject.toJSONString(jsonObject));
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("获取token调用结果：{}", jsonData);
        if (1 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), jsonData.getString("msg"));
        }
        JSONObject data = jsonData.getJSONObject("data");
        System.out.println(data);
        return data.getString("token");
    }

    @Test
    public void testGetToken() {
        String token = getToken("http://47.94.245.234:9091/","40281449");
        System.out.println(token);
    }

    /**
     * 上传塔吊参数
     * @param
     * @param
     */
    public void reportFseCraneParam(ZHGJFseCraneParamDTO zhgjFseCraneParamDTO, FseSync fseSync) throws BizException{
        logger.info("智慧工匠塔吊设备参数：{}", zhgjFseCraneParamDTO.toString());
        String path = "/api/device/config/towerCrane";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceSn",zhgjFseCraneParamDTO.getSn());
        jsonObject.put("deviceType",2);
        jsonObject.put("data",zhgjFseCraneParamDTO);
        Map<String, String> headers = new HashMap<>();
        String token = getToken(fseSync.getHost(), zhgjFseCraneParamDTO.getSn());
        headers.put("token",token);
        String result = HttpUtil.doPost(fseSync.getHost() + path, JSONObject.toJSONString(jsonObject),headers);
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("智慧工匠塔吊设备参数上传结果：{}", jsonData);
        if (1 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), jsonData.getString("msg"));
        }
    }

    /**
     * 上传塔机实时数据
     * @param
     * @param fseSync
     */
    public void reportFseCraneRealData(ZHGJFseCraneDTO zhgjFseCraneDTO, FseSync fseSync) throws BizException{
        logger.info("智慧工匠塔机实时数据推送：{}", zhgjFseCraneDTO.toString());
        String path = "/api/device/data/towerCrane";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceSn",zhgjFseCraneDTO.getSn());
        jsonObject.put("deviceType",2);
        jsonObject.put("data",zhgjFseCraneDTO);
        Map<String, String> headers = new HashMap<>();
        String token = getToken(fseSync.getHost(), zhgjFseCraneDTO.getSn());
        headers.put("token",token);
        String result = HttpUtil.doPost(fseSync.getHost() + path, JSONObject.toJSONString(jsonObject),headers);
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("智慧工匠塔机实时数据上传结果：{}", jsonData);
        if (1 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), jsonData.getString("msg"));
        }
    }


    /**
     * 上传升降机参数
     * @param
     * @param
     */
    public void reportFseLiftParam(ZHGJFseLiftParamDTO zhgjFseLiftParamDTO, FseSync fseSync) throws BizException{
        logger.info("智慧工匠升降机设备参数：{}", zhgjFseLiftParamDTO.toString());
        String path = "/api/device/config/elevator";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceSn",zhgjFseLiftParamDTO.getSn());
        jsonObject.put("deviceType",3);
        jsonObject.put("data",zhgjFseLiftParamDTO);
        Map<String, String> headers = new HashMap<>();
        String token = getToken(fseSync.getHost(), zhgjFseLiftParamDTO.getSn());
        headers.put("token",token);
        String result = HttpUtil.doPost(fseSync.getHost() + path, JSONObject.toJSONString(jsonObject),headers);
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("智慧工匠升降机设备参数上传结果：{}", jsonData);
        if (1 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), jsonData.getString("msg"));
        }
    }

    /**
     * 上传升降机实时数据
     * @param
     * @param fseSync
     */
    public void reportFseLiftRealData(ZHGJFseLiftDTO zhgjFseLiftDTO, FseSync fseSync) throws BizException{
        logger.info("智慧工匠升降机实时数据推送：{}", zhgjFseLiftDTO.toString());
        String path = "/api/device/data/elevator";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceSn",zhgjFseLiftDTO.getSn());
        jsonObject.put("deviceType",3);
        jsonObject.put("data",zhgjFseLiftDTO);
        Map<String, String> headers = new HashMap<>();
        String token = getToken(fseSync.getHost(), zhgjFseLiftDTO.getSn());
        headers.put("token",token);
        String result = HttpUtil.doPost(fseSync.getHost() + path, JSONObject.toJSONString(jsonObject),headers);
        JSONObject jsonData = JSONObject.parseObject(result);
        logger.info("智慧工匠升降机实时数据上传结果：{}", jsonData);
        if (1 != jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(), jsonData.getString("msg"));
        }
    }

    @Test
    public void testReportDust() {
        FseSync envSync = new FseSync();
        envSync.setHost("http://47.94.245.234:9091/");
        ZHGJFseCraneDTO zhgjFseCraneDTO = new ZHGJFseCraneDTO();
        zhgjFseCraneDTO.setSn("40281449");
        reportFseCraneRealData(zhgjFseCraneDTO, envSync);
    }

}
